/**
 * 工具函数
 */


//手机号验证
function isPhone(str) {
    return !(/^1[3-9]\d{9}$/.test(str))
}




//轻提示toast

function showToast(status, text) {
    //创建div
    const div = document.createElement('div')

    //添加类名
    div.className = `toast dpflex jccenter fdcolumn tac f20`

    //添加内容
    div.innerHTML = `
        <i class="f38 mb8 iconfont icon-${status === 'success' ? 'success' : 'fail'}"></i>
        <span>${text}</span>
    `

    //添加到网页
    document.body.append(div)

    setTimeout(function () {
        div.remove();//删除自己
    }, 2000)
}


//运动头部导航
function sportsTopNav(name){
    //创建div
    const div = document.createElement('div')

    //添加类名
    div.className =`wrapper dpflex header`

    //添加内容
    div.innerHTML=`
        <a href="./running.html" class="tac spanC" id="${name === 'running' ? 'spanC' : ''}">跑步</a>
        <a href="./riding.html" class="tac" id="${name === 'riding' ? 'spanC' : ''}">骑行</a>
        <a href="./course.html" class="tac" id="${name === 'course' ? 'spanC' : ''}">课程训练</a>
    `
    //添加到网页中
    document.body.prepend(div)
}







//底部动态页脚
function showTabs(tab) {
    //创建div
    const div = document.createElement('div')

    //添加类名
    div.className = `tabs dpflex`

    //添加内容
    div.innerHTML = `
        <a href="./home.html" class="dpflex fdcolumn f14 tac" id="${tab === 'home' ? 'active' : ''}">
            <i class="iconfont icon-home"></i>
            <p>首页</p>
        </a>
        <a href="./running.html" class="dpflex fdcolumn f14 tac" id="${tab === 'sports' ? 'active' : ''}">
            <i class="iconfont icon-sport"></i>
            <p>运动</p>
        </a>
        <a href="./mine.html" class="dpflex fdcolumn f14 tac" id="${tab === 'mine' ? 'active' : ''}">
            <i class="iconfont icon-mine"></i>
            <p>我的</p>
        </a>
    `

    //添加到网页中
    document.body.append(div)

}



//补零函数
function polishing(v){
   return v>=10?v:`0${v}`
}



//判断用户是否登录
function isLogin(){
    //获取本地存储中的userId
    let userId=localStorage.getItem('userId')

    //如果没有登入，则跳回登录页
    if(!userId) location.href='./enter.html'
        
}

//处理时间
function formatTime(t){
    let h = Math.floor(t/60/60);
    let m = Math.floor(t/60)%60;
    let s = t%60;
    return [h,m,s].map(function (item){
        return polishing(item)
    }).join(':')
}




//根据经纬度计算距离
//startPoint:  第一个点
//curPoint:  第二个点

function calcDistance(startPoint, curPoint) {
    let lat1 = startPoint.lat
    let lng1 = startPoint.lng

    let lat2 = curPoint.lat
    let lng2 = curPoint.lng

    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s.toFixed(2) // 单位千米
}

//计算配速函数 
//d： 距离
//s： 时间
function calcPace(d, s) {
    let distance = d
    let spendTime = s / 60
    if (distance > 0 && spendTime > 0) {
        let pace = parseFloat(spendTime / distance)
        let m = Math.floor(pace)
        let s = ((pace - m) * 60).toFixed(0)
        return `${m}'${s}"`
    }
    return "--"
}



//计算卡路里
function calCalorie(miles) {
    if (miles > 0) {
        let calorie = 60 * miles * 1.036;
        return calorie.toFixed(2)
    }
    return "--"
}



//地图添加点
//amap: 地图
//point: 点(经纬度)
//pic: 图标
function addMarker(amap, point, pic) {
    // 创建图标对象   
    const myIcon = new BMap.Icon(pic, new BMap.Size(45, 45), {//图标尺寸    
        anchor: new BMap.Size(15, 30), // 设置偏移量
        imageSize: new BMap.Size(30, 32)   // 设置图片大小  
    });
    // 创建标注对象并添加到地图   
    var marker = new BMap.Marker(point, { icon: myIcon });

    //将标注对象添加到某个具体的地图上
    amap.addOverlay(marker);
}




//地图添加折线

function addPolyLine(amap,prevPoint, nextPoint) {

    //定义折线对象
    const polyline = new BMap.Polyline([
        new BMap.Point(prevPoint.lng, prevPoint.lat),//第一点
        new BMap.Point(nextPoint.lng, nextPoint.lat)//第二点
    ],  //线条样式
        { strokeColor: "blue", strokeWeight: 6, strokeOpacity: 0.5 }
    );
    //将折线对象添加地图上
    amap.addOverlay(polyline);
}

//暴露对象
window.until = {
    isPhone,
    showToast,
    showTabs,
    polishing,
    isLogin,
    sportsTopNav,
    formatTime,
    calcDistance,
    calcPace,
    calCalorie,
    addMarker,
    addPolyLine
}